//
//  LCYProductDetailViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/23.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYProductDetailViewController.h"
#import "LCYNetWorkRequestManager.h"

@interface LCYProductDetailViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation LCYProductDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.proDetailArray.title;
    
    self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:self.webView];
    
    [self loadData];
    
}
- (void)loadData {
    [LCYNetWorkRequestManager requestURL:[NSURL URLWithString:SHOPDETAILLIST_URL] RequestMethod:LCY_POST Params:@{@"auth":@"XZU7RH7M1861DC8Z8H8HvkTJxQMGO9zo4XDA0cWP22NdFSh9d7fo",@"client":@"1",@"contentid":self.proDetailArray.contentid,@"deviceid":@"6D4DD967-5EB2-40E2-A202-37E64F3BEA31",@"version":@"3.0.6"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
//        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        if (data) {
            NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = jsonDic[@"data"];
            NSDictionary *postsinfoDic = dataDic[@"postsinfo"];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.webView loadHTMLString:postsinfoDic[@"html"] baseURL:nil];
            });
        }
        
    }];

}


#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"加载完成");
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=380;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
